﻿using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Mt.Site.Core.Base.Authorization;
using Mt.Site.Core.Base.Authorization.Users;
using Mt.Site.EntityFrameworkCore;
using System.Linq;

namespace Mt.Site.Migrations.Seed.Host
{
    public class HostRoleAndUserCreator
    {
        private readonly MtSiteDbContext _context;

        public HostRoleAndUserCreator(MtSiteDbContext context)
        {
            _context = context;
        }

        public void Create()
        {
            CreateHostRoleAndUsers();
        }

        private void CreateHostRoleAndUsers()
        {
            //admin user for host

            //一个系统管理员
            var systemUserForHost = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == null && u.UserName == User.SystemUserName);
            if (systemUserForHost == null)
            {
                systemUserForHost = UserFunction.CreateSystemUser(null, "system@aspnet.com");
                systemUserForHost.Password = new PasswordHasher<User>(new OptionsWrapper<PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(systemUserForHost, "bnm970");
                systemUserForHost.IsEmailConfirmed = true;
                systemUserForHost.ShouldChangePasswordOnNextLogin = false;
                systemUserForHost.IsActive = true;
                systemUserForHost.Order = 2;
                _context.Users.Add(systemUserForHost);
                _context.SaveChanges();

                //User account of system user
                //_context.UserAccounts.Add(new UserAccount
                //{
                //    TenantId = null,
                //    UserId = systemUserForHost.Id,
                //    UserName = systemUserForHost.Name,
                //    EmailAddress = systemUserForHost.EmailAddress
                //});
                //_context.SaveChanges();
            }

            //一个超级管理员
            var adminUserForHost = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == null && u.UserName == User.AdminUserName);
            if (adminUserForHost == null)
            {
                adminUserForHost = UserFunction.CreateAdminUser(null, "admin@aspnet.com");
                adminUserForHost.Password = new PasswordHasher<User>(new OptionsWrapper<PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(adminUserForHost, "123qwe");
                adminUserForHost.IsEmailConfirmed = true;
                adminUserForHost.ShouldChangePasswordOnNextLogin = false;
                adminUserForHost.IsActive = true;
                adminUserForHost.Order = 3;
                _context.Users.Add(adminUserForHost);
                _context.SaveChanges();

                //User account of admin user
                //_context.UserAccounts.Add(new UserAccount
                //{
                //    TenantId = null,
                //    UserId = adminUserForHost.Id,
                //    UserName = adminUserForHost.Name,
                //    EmailAddress = adminUserForHost.EmailAddress
                //});
                //_context.SaveChanges();
            }

            var mountorUserForHost = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == null && u.UserName == User.SuperUserName);
            if (mountorUserForHost == null)
            {
                mountorUserForHost = UserFunction.CreateMountorUser(null, "mountor@aspnet.com");
                mountorUserForHost.Password = new PasswordHasher<User>(new OptionsWrapper<PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(systemUserForHost, "bnm970");
                mountorUserForHost.IsEmailConfirmed = true;
                mountorUserForHost.ShouldChangePasswordOnNextLogin = false;
                mountorUserForHost.IsActive = true;
                mountorUserForHost.Order = 1;
                _context.Users.Add(mountorUserForHost);
                _context.SaveChanges();
            }
        }
    }
}
